# 导入 pymongo 数据库
from pymongo import MongoClient

# 初始化 MongoDb

client = MongoClient ('mongodb://stock88:123456@www.yueshushu.top:27017')

databaseName = 'stock'
collectionName= 'python1'

database = client[databaseName]
collection = database[collectionName]

# 插入数据

'''
data = {'id':1, 'name': '岳泽霖', 'age':30}
collection.insert_one(data)
print('插入数据库成功')

# 插入多条数据
dataList = [
{'id':2, 'name': '岳泽霖', 'age':30},
{'id':3, 'name': '岳泽霖', 'age':30},
{'id':4, 'name': '岳泽霖', 'age':30}
]
collection.insert_many(dataList)
'''
# 查询字段
'''
idInfo = collection.find_one({'id':2})
print(f'id 为2的数据 : {idInfo}')
allList = collection.find()
for info in allList:
    print(f'查询全部: {info}')
ageList = collection.find({'age':30,'id':2})
for ageInfo in ageList:
    print(f'多条件查询: {ageInfo}')
# 查询时排序  -1为降序 1为升序
allList2 = collection.find({'age':30}).sort('id',-1)
for info in allList2:
    print(f'降序: {info}')
'''

'''
# 更新一条记录
collection.update_one({'id':1},{'$set':{'name':'岳建立'}})
collection.update_many({'age':30},{'$set':{'age':28}})
'''

'''
# 删除记录
collection.delete_one({'id':3})
collection.delete_many({'id':3})
'''
# 对结果进行去重
ageList = collection.distinct('age')
for info in ageList:
    print(f'年龄: {info}')